package com.fa.oj.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.fa.oj.model.dto.question.JudgeCase;
import com.fa.oj.model.dto.question.JudgeConfig;
import com.fa.oj.judge.codesandbox.model.JudgeInfo;
import com.fa.oj.model.entity.Question;
import com.fa.oj.model.enums.JudgeInfoMessageEnum;

import java.util.List;

/**
 * ClassName: DefaultJudgeStrategy
 * Package: com.fa.oj.judge.strategy
 * Description: 默认判题策略
 *
 * @Author:
 * @Create: 2024/4/23 - 20:34
 * @Version: v1.0
 */
public class DefaultJudgeStrategy implements JudgeStrategy{

    /**
     * 执行判题
     * @param judgeContext
     * @return
     */
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();

        List<String> inputList = judgeContext.getInputList();
        List<String> outputListByResponse = judgeContext.getOutputList();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        Question question = judgeContext.getQuestion();

        Long memory = judgeInfo.getMemory();
        Long time = judgeInfo.getTime();
        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;

        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setTime(time);

        if (outputListByResponse.size() != inputList.size()) {
            // 执行结果的输出用例与输入用例个数不符，说明 答案不正确。修改数据库的判题信息
            judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        //个数相同再判断结果的输出 和 用例的输出是否一致
        for (int i = 0; i < judgeCaseList.size(); i++) {
            JudgeCase judgeCase = judgeCaseList.get(i);
            if (!outputListByResponse.get(i).equals(judgeCase.getOutput())) {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }
        }
        // 执行结果的输出 与 用例的输出一致， 还要判断 是否满足题目限制
        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig judgeConfigObj = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        Long timeLimit = judgeConfigObj.getTimeLimit();
        Long memoryLimit = judgeConfigObj.getMemoryLimit();

        if (time > timeLimit) {
            judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        if (memory > memoryLimit) {
            judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        // 成功就设置 InfoMessageEnum为 通过
        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
        return judgeInfoResponse;
    }
}
